package com.gzx.plugin.tjzy.modular.sms.reply.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fhs.trans.service.impl.TransService;
import com.google.common.collect.Lists;
import com.gzx.plugin.tjzy.modular.sms.reply.bo.GzxSmsReplyQueryBo;
import com.gzx.plugin.tjzy.modular.sms.reply.entity.GzxSmsReply;
import com.gzx.plugin.tjzy.modular.sms.reply.mapper.GzxSmsReplyMapper;
import com.gzx.plugin.tjzy.modular.sms.reply.service.GzxSmsReplyService;
import com.gzx.plugin.tjzy.modular.sms.reply.vo.GzxSmsReplyVo;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import vip.xiaonuo.common.consts.IntConstant;
import vip.xiaonuo.common.util.CommonCryptogramUtil;
import vip.xiaonuo.common.util.CommonExcelUtil;

import java.util.List;
import java.util.stream.Collectors;

import static org.apache.commons.lang.StringEscapeUtils.escapeSql;

/**
 * @author zjt
 * @description
 * @date 2025/7/25 14:22
 */
@Service
@DS("sms")
@RequiredArgsConstructor
public class GzxSmsReplyServiceImpl implements GzxSmsReplyService {

    private final GzxSmsReplyMapper smsReplyMapper;

    private final TransService transService;

    /**
     * 短信回复分页查询
     *
     * @param smsReplyQueryBo
     * @return
     */
    @Override
    public Page<GzxSmsReplyVo> pageList(GzxSmsReplyQueryBo smsReplyQueryBo) {
        return smsReplyMapper.selectVoPage(new Page<>(smsReplyQueryBo.getCurrent(), smsReplyQueryBo.getSize()), buildQueryWrapper(smsReplyQueryBo));
    }

    /**
     * 导出短信回复
     *
     * @param smsReplyQueryBo
     */
    @Override
    public void export(GzxSmsReplyQueryBo smsReplyQueryBo, HttpServletResponse httpServletResponse) {
        List<GzxSmsReplyVo> smsReplyVoList = smsReplyMapper.selectVoList(buildQueryWrapper(smsReplyQueryBo));
        transService.transMore(smsReplyVoList);
        CommonExcelUtil.exportExcel("短信回复数据", smsReplyVoList, GzxSmsReplyVo.class, httpServletResponse);
    }

    /**
     * 批量保存短信回复
     *
     * @param smsReplyList
     */
    @Override
    public void insertBatch(List<GzxSmsReply> smsReplyList) {
        List<List<GzxSmsReply>> batchList = Lists.partition(smsReplyList, IntConstant.VALUE_1000);
        for (List<GzxSmsReply> batch : batchList) {
            String batchSql = batch.stream()
                    .map(s -> String.format("('%s', '%s','%s', '%s', '%s', '%s', '%s', '%s', '%s','%s', %s)",
                            escapeSql(s.getSupplierCode()),
                            escapeSql(s.getBatchNumber()),
                            escapeSql(s.getProjectCode()),
                            escapeSql(s.getMsgId()),
                            escapeSql(CommonCryptogramUtil.doSm1AesEncrypt(s.getPhone())),
                            escapeSql(s.getReplyTime()),
                            escapeSql(s.getReplyContent()),
                            escapeSql(s.getSendTime()),
                            escapeSql(s.getSendContent()),
                            escapeSql(s.getReplyDetail()),
                            escapeSql(String.valueOf(s.getTenantId()))
                    ))
                    .collect(Collectors.joining(", ")).replace("'null'", "null");
            smsReplyMapper.insertBatch(batchSql);
        }
    }

    /**
     * 构建查询条件
     *
     * @param smsReplyQueryBo
     * @return
     */
    private LambdaQueryWrapper<GzxSmsReply> buildQueryWrapper(GzxSmsReplyQueryBo smsReplyQueryBo) {
        return Wrappers.<GzxSmsReply>lambdaQuery()
                .eq(StringUtils.isNotBlank(smsReplyQueryBo.getSupplierCode()), GzxSmsReply::getSupplierCode, smsReplyQueryBo.getSupplierCode())
                .eq(StringUtils.isNotBlank(smsReplyQueryBo.getProjectCode()), GzxSmsReply::getProjectCode, smsReplyQueryBo.getProjectCode())
                .eq(StringUtils.isNotBlank(smsReplyQueryBo.getPhone()), GzxSmsReply::getPhone, CommonCryptogramUtil.doSm1AesEncrypt(smsReplyQueryBo.getPhone()))
                .between(StringUtils.isNotBlank(smsReplyQueryBo.getBeginTime()) && StringUtils.isNotBlank(smsReplyQueryBo.getEndTime()), GzxSmsReply::getCreateTime, smsReplyQueryBo.getBeginTime(), smsReplyQueryBo.getEndTime())
                .orderByDesc(GzxSmsReply::getId);
    }
}
